package com.samoilovich.belarustoday.basefragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.datemodel.EventModel;
import com.samoilovich.belarustoday.datemodel.NewsModel;
import com.samoilovich.belarustoday.mainscreen.MainActivity;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class BaseFragment extends Fragment {
    private String TAG = getClass().getSimpleName();

    protected boolean mInProgress;
    protected View mRootView;
    protected boolean mHideOptionsMenu = false;
    private InternetBroadcastReceiver mReceiver;
    private String mTitle;
    private IntentFilter mFilter;
    private boolean mAnimationFlag = false;

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    @Override
    public void onResume() {
        super.onResume();
        registerInternetBroadcastReceiver();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mAnimationFlag) {
            Utility.addAppearAnimation(mRootView);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        this.getActivity().unregisterReceiver(mReceiver);
    }

    public void update(final ArrayList<CategoryModel> categories) {
    }

    public void updateCategoryContent(final ArrayList<NewsModel> news) {
    }

    public void updateNewsContent(NewsModel newsModel) {
    }

    public void updateEventList(final ArrayList<EventModel> events) {
    }

    // This method are calling by BaseFragmentActivity when user clicked 'Update' button in the option menu.
    public void updateAllData(Context context) {

    }

    public void prepareActionBarViews(int title, int backStrRes, boolean addAnimation) {
        mTitle = getString(title);
        ((TextView) mRootView.findViewById(R.id.textView_backLabel)).setText(backStrRes);
        ((TextView) mRootView.findViewById(R.id.textView_title)).setText(title);
        mAnimationFlag = addAnimation;
        mRootView.clearAnimation();
        prepareActionBar(addAnimation);
    }

    public void prepareActionBarViews(String title, int backStrRes, boolean addAnimation) {
        mTitle = title;
        ((TextView) mRootView.findViewById(R.id.textView_backLabel)).setText(backStrRes);
        ((TextView) mRootView.findViewById(R.id.textView_title)).setText(title);
        mAnimationFlag = addAnimation;
        mRootView.clearAnimation();
        prepareActionBar(addAnimation);
    }

    public void prepareActionBarViews(int title, String backStrRes, boolean addAnimation) {
        mTitle = getString(title);
        ((TextView) mRootView.findViewById(R.id.textView_backLabel)).setText(backStrRes);
        ((TextView) mRootView.findViewById(R.id.textView_title)).setText(title);
        mAnimationFlag = addAnimation;
        mRootView.clearAnimation();
        prepareActionBar(addAnimation);
    }

    public void prepareActionBarViews(String title, String backStrRes, boolean addAnimation) {
        mTitle = title;
        ((TextView) mRootView.findViewById(R.id.textView_backLabel)).setText(backStrRes);
        ((TextView) mRootView.findViewById(R.id.textView_title)).setText(title);
        mAnimationFlag = addAnimation;
        mRootView.clearAnimation();
        prepareActionBar(addAnimation);
    }

    // -----------------------------------------------------------------------------------
    protected void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void prepareActionBar(final boolean addHideAnimation) {
        RelativeLayout backView = (RelativeLayout) mRootView.findViewById(R.id.relativeLayout_back);

        (mRootView.findViewById(R.id.button_option)).setVisibility(View.VISIBLE);
        backView.setVisibility(View.VISIBLE);
        backView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (addHideAnimation) {
                    Log.e("Tag", BaseFragment.this.toString());
                    Utility.addHideAnimation(mRootView);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (getActivity() != null) {
                                getActivity().onBackPressed();
                            }
                        }
                    }, Constants.ANIMATION_DURATION);
                } else {
                    getActivity().onBackPressed();
                }
            }
        });

        Button optionsButton = (Button) mRootView.findViewById(R.id.button_option);
        View divider = mRootView.findViewById(R.id.view_actionBarButtonDivider);
        if (mHideOptionsMenu) {
            optionsButton.setVisibility(View.GONE);
            divider.setVisibility(View.GONE);
        } else {
            optionsButton.setVisibility(View.VISIBLE);
            divider.setVisibility(View.VISIBLE);
        }
        optionsButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                ((BaseFragmentActivity) getActivity()).setOptionsFragment(mTitle);
            }
        });
    }

    private void registerInternetBroadcastReceiver() {
        if (mReceiver == null) {
            mReceiver = new InternetBroadcastReceiver();
        }
        if (mFilter == null) {
            mFilter = new IntentFilter();
            mFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            mFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
            mFilter.addAction(Constants.PICTURE_LOADING_ACTION);
        }
        this.getActivity().registerReceiver(mReceiver, mFilter);
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private class InternetBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean state = Utility.hasInternetConnection(getActivity());
            if (getActivity() != null) {
                BaseFragmentActivity activity = ((BaseFragmentActivity) getActivity());
                if (!(activity instanceof MainActivity)) {
                    activity.mUpdateButtonState = state;
                }
            }
            onReceiveInternetIntent(state, intent);
        }
    }
}
